package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.dto.StationResponse;
import com.ugoodtech.mdcc.core.helper.SearchVoId;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.DepartmentCoreService;
import com.ugoodtech.mdcc.core.service.SearchPermissionsService;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.StationService;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StationServiceImpl implements StationService {
    @Autowired
    private StationRepository stationRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private DepartmentCoreService departmentCoreService;

    @Autowired
    private SearchPermissionsService searchPermissionsService;


    private Page<StationReport> getStationReport(Long customerStat,
                                                 Long customerType,
                                                 String paintBrand,
                                                 String productPosition,
                                                 String province,
                                                 String city,
                                                 String category,
                                                 String brand,
                                                 String param,
                                                 String stationNum,
                                                 Pageable pageable,
                                                 Long loginUserId,
                                                 String userName,
                                                 Long departmentId) {
        User user = userRepository.findOne(loginUserId);
        UserProfile userProfile = userProfileRepository.findByUserId(loginUserId);
        SearchVoId searchVoId;
        if (user.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(userProfile, province, brand);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(userProfile, province, brand);
        }
        if (searchVoId.getResultStationReportId().isEmpty()) {
            return new PageImpl(new ArrayList(), pageable, 0L);
        }
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.station.checkStation.eq(true));
        builder.and(qStationReport.user.isNotNull());
        builder.and(qStationReport.station.province.isNotNull());
        builder.and(qStationReport.station.city.isNotNull());
        builder.and(qStationReport.station.deleted.eq(false));
        builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
        builder.and(qStationReport.station.contact.isNotNull());
        builder.and(qStationReport.id.in(searchVoId.getResultStationReportId()));
        if (StringUtils.isNotEmpty(userName) || departmentId != null && departmentId != 0) {
            List<Long> parementDeparetment = departmentCoreService.parentDepartment();
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
            boolean flag = false;
            List<Long> idList = new ArrayList<>();
            if (departmentId != null && departmentId != 0) {
                flag = true;
                Department department = null;
                department = departmentRepository.findOne(departmentId);
                if (department != null) {
                    if (parementDeparetment.contains(departmentId)) {
                        List<Long> lowerDepartment = departmentCoreService.getLowerDepartment(departmentId);
                        idList.addAll(lowerDepartment);
                    } else {
                        idList.add(departmentId);
                    }
                }
                booleanBuilder.and(qUserProfile.departmentList.any().id.in(idList));
            }
            if (StringUtils.isNotEmpty(userName)) {
                flag = true;
                booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            }
            if (flag) {
                List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
                idList = new ArrayList<>();
                for (UserProfile profile : userProfileList) {
                    idList.add(profile.getUser().getId());
                }
                builder.and(qStationReport.user.id.in(idList));
            }
        }

        if (StringUtils.isNotEmpty(stationNum)) {
            builder.and(qStationReport.station.stationNum.like("%" + stationNum + "%"));
        }
        if (customerStat != null && customerStat != 0) {
            builder.and(qStationReport.station.customerStat.eq(customerStat));
        }
        if (customerType != null && customerType != 0) {
            builder.and(qStationReport.station.customerType.eq(customerType));
        }
        if (StringUtils.isNotEmpty(paintBrand)) {
            builder.and(qStationReport.station.paintBrand.like("%" + paintBrand + "%"));
        }
        if (StringUtils.isNotEmpty(productPosition)) {
            builder.and(qStationReport.station.productPosition.like("%" + productPosition + "%"));
        }
        if (StringUtils.isNotEmpty(city)) {
            builder.and(qStationReport.station.city.eq(city));
        }
        if (StringUtils.isNotEmpty(category)) {
            builder.and(qStationReport.station.category.like("%" + category + "%"));
        }
        if (StringUtils.isNotEmpty(param)) {
            builder.and(qStationReport.station.name.like("%" + param + "%"));
        }
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        if (null == pageable) {
            pageable = new PageRequest(0, 100000000, sort);
        } else {
            pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        }
        return stationReportRepository.findAll(builder, pageable);
    }

    @Override
    public JsonResponse queryStation(Long customerStat,
                                     Long customerType,
                                     String paintBrand,
                                     String productPosition,
                                     @ApiParam("省份") String province,
                                     @ApiParam("城市") String city,
                                     @ApiParam("维修站类型") String category,
                                     @ApiParam("品牌") String brand,
                                     @ApiParam("关键字") String param,
                                     String stationNum,
                                     Pageable pageable,
                                     Long loginUserId,
                                     String userName,
                                     Long departmentId) {
        Page<StationReport> stationReports = getStationReport(customerStat, customerType, paintBrand, productPosition, province, city,
                category, brand, param, stationNum, pageable, loginUserId, userName, departmentId);
        List<Station> stationList = new ArrayList<>();
        for (StationReport report : stationReports.getContent()) {
            stationList.add(report.getStation());
        }
        List<StationResponse> stationResponseList = toStationResponse(stationList);
        Page<Station> page = new PageImpl(stationResponseList, pageable, stationReports.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    public List<StationResponse> toStationResponse(List<Station> stations) {
        List<StationResponse> stationResponseList = new ArrayList<>();
        for (Station station : stations) {
            StationResponse response = new StationResponse(station);
            QStationReport qStationReport = QStationReport.stationReport;
            String[] brandArray = station.getBrand().split(",");
            StringBuffer stringBuffer = new StringBuffer("");
            StringBuffer otherStringBuffer = new StringBuffer("");
            for (String brand : brandArray) {
                if (StringUtils.isNotEmpty(brand)) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                    if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                        stringBuffer.append(brand + ",");
                    } else {
                        otherStringBuffer.append(brand + ",");
                    }
                }
            }
            if (otherStringBuffer != null && StringUtils.isNotEmpty(otherStringBuffer.toString())) {
                stringBuffer.append("其他（" + otherStringBuffer.toString() + "）");
            }
            response.setBrand(stringBuffer.toString());
            BooleanBuilder builder = new BooleanBuilder();
            builder.and(qStationReport.station.id.eq(station.getId()));
            builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
            StationReport stationReport = stationReportRepository.findOne(builder);
            response.setDateTimeStr(stationReport.getCreationTimeStr());
            if (stationReport != null) {
                response.setReportId(stationReport.getId());
                StationColorReport colorReport = stationColorReportRepository.selectReport(stationReport.getId(), 14L);
                if (colorReport != null) {
                    response.setDealersName(colorReport.getAnswerOther());
                }
            }
            if (station.getCustomerStat() != null && station.getCustomerStat() != 0) {
                Long statId = station.getCustomerStat();
                if (statId == 362L) {
                    StationColorReport colorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                    if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
                        response.setCustomerStat(colorReport.getAnswerOther());
                    }
                } else {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(statId);
                    if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                        response.setCustomerStat(answerOption.getName());
                    }
                }
            }
            if (station.getCustomerType() != null && station.getCustomerType() != 0) {
                Long typeId = station.getCustomerType();
                if (typeId == 366L) {
                    StationColorReport colorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                    if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
                        response.setCustomerType(colorReport.getAnswerOther());
                    }
                } else {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(typeId);
                    if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                        response.setCustomerType(answerOption.getName());
                    }
                }
            }
            User user = stationReport.getUser();
            UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
            if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
                response.setUserName(userProfile.getRealName());
            } else {
                response.setUserName("");
            }
            stationResponseList.add(response);
        }
        return stationResponseList;
    }

    private Page<Station> setColorReport(Page<Station> stationPage, Pageable pageable) {
        List<Station> stationList = new ArrayList<>();
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder;
        for (Station station : stationPage) {
            builder = new BooleanBuilder();
            builder.and(qStationReport.station.id.eq(station.getId()));
            builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
            StationReport stationReport = stationReportRepository.findOne(builder);
            if (stationReport != null) {
                station.setColorReportId(stationReport.getId());
            }
            stationList.add(station);
        }
        Sort sort = new Sort(Sort.Direction.DESC, "creationTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<Station> page = new PageImpl(stationList, pageable, stationPage.getTotalElements());
        return page;
    }


    /**
     * 查询、检查是否有此维修站
     *
     * @param province    省份
     * @param city        城市
     * @param category    类型
     * @param brand       品牌
     * @param stationName 维修站名字
     * @return
     */
    @Override
    public JsonResponse checkStation(User user, String province, String city, String category, String brand, String stationName) {
        UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
        if (userProfile == null) {
            return JsonResponse.errorResponseWithError("抱歉", "当前用户信息不完整");
        }
        List<Nation> nationList = userProfile.getNationList();
        List<Brand> brandList = userProfile.getBrandList();
        QStation qStation = QStation.station;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStation.enabled.eq(true));
        builder.and(qStation.deleted.eq(false));
        builder.and(qStation.checkStation.eq(true));
        builder.and(qStation.user.isNotNull());
        builder.and(qStation.province.isNotNull());
        builder.and(qStation.city.isNotNull());
        builder.and(qStation.contact.isNotNull());
        if (StringUtils.isNotEmpty(province)) {
            builder.and(qStation.province.like(province));
        }
//        else {
//            List<String> provinceStr = new ArrayList<>();
//            for (Nation nation : nationList) {
//                provinceStr.add(nation.getProvince());
//            }
//            builder.and(qStation.province.in(provinceStr));
//        }
        if (StringUtils.isNotEmpty(city)) {
            builder.and(qStation.city.eq(city));
        }
        if (StringUtils.isNotEmpty(category)) {
            builder.and(qStation.category.eq(category));
        }
        if (StringUtils.isNotEmpty(brand)) {
            builder.and(qStation.brand.like("%" + brand + "%"));
        }
//        else {
//            List<String> brandStr = new ArrayList<>();
//            for (Brand b : brandList) {
//                brandStr.add(b.getBrandName());
//            }
//            builder.and(qStation.brand.in(brandStr));
//        }
        if (StringUtils.isNotEmpty(stationName)) {
            builder.and(qStation.name.like("%" + stationName + "%"));
        }
        List<Station> station = (List<Station>) stationRepository.findAll(builder);
        if (station == null) {
            return JsonResponse.errorResponseWithError("500", "抱歉，没有找到维修站!");
        }
        List<StationResponse> list = toObjectList(station);
        return JsonResponse.successResponseWithData(list);
    }


    private List<StationResponse> toObjectList(List<Station> stations) {
        List<StationResponse> list = new ArrayList<>();
        for (Station station : stations) {
            StationResponse response = new StationResponse();
            response.setStationId(station.getId());
            response.setName(station.getName());
            list.add(response);
        }
        return list;

    }

    @Override
    public JsonResponse exportToExcel(@ApiParam("客户状态") Long customerStat,
                                      @ApiParam("客户类型") Long customerType,
                                      @ApiParam("油漆品牌") String paintBrand,
                                      @ApiParam("品牌定位") String productPosition,
                                      @ApiParam("省份") String province,
                                      @ApiParam("城市") String city,
                                      @ApiParam("维修站类型") String category,
                                      @ApiParam("品牌") String brand,
                                      @ApiParam("关键字") String param,
                                      @ApiParam("维修站编号") String stationNum,
                                      Long loginUserId,
                                      @ApiParam("技术人员姓名") String userName,
                                      @ApiParam("部门ID") Long departmentId) {
        Page<StationReport> stationReports = getStationReport(customerStat, customerType, paintBrand, productPosition, province, city,
                category, brand, param, stationNum, null, loginUserId, userName, departmentId);
        List<List<String>> stationInfoList = getStationInfoList(stationReports.getContent());
        return JsonResponse.successResponseWithData(stationInfoList);
    }


    private List<List<String>> getStationInfoList(List<StationReport> stationReports) {
        List<List<String>> stationInfoLists = new ArrayList<>();
        String value = "";
        UserProfile userProfile = null;
        for (StationReport report : stationReports) {
            List<String> stationInfoList = new ArrayList<>();
            //获得创建人
            User user = report.getUser();
            if (user != null) {
                userProfile = userProfileRepository.findByUserId(user.getId());
                if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
                    stationInfoList.add(userProfile.getRealName());
                } else {
                    stationInfoList.add(value);
                }
            } else {
                stationInfoList.add(value);
            }
            //创建时间
            stationInfoList.add(report.getCreationTimeStr());
            //最近一次编辑人
            user = report.getUser();
            if (user != null) {
                userProfile = userProfileRepository.findByUserId(user.getId());
                if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
                    stationInfoList.add(userProfile.getRealName());
                } else {
                    stationInfoList.add(value);
                }
            } else {
                stationInfoList.add(value);
            }
            //最近一次修改时间
            stationInfoList.add(report.getUpdateTimeStr());

            Station station = report.getStation();
            if (station != null) {
                //获得维修站ID
                if (StringUtils.isNotEmpty(station.getStationNum())) {
                    stationInfoList.add(station.getStationNum());
                } else {
                    stationInfoList.add(value);
                }
                //获得站点名称
                if (StringUtils.isNotEmpty(station.getName())) {
                    stationInfoList.add(station.getName());
                } else {
                    stationInfoList.add(value);
                }
                //获得省份
                if (StringUtils.isNotEmpty(station.getProvince())) {
                    stationInfoList.add(station.getProvince());
                } else {
                    stationInfoList.add(value);
                }
                //获得城市
                if (StringUtils.isNotEmpty(station.getCity())) {
                    stationInfoList.add(station.getCity());
                } else {
                    stationInfoList.add(value);
                }
                //获得地址
                if (StringUtils.isNotEmpty(station.getAddress())) {
                    stationInfoList.add(station.getAddress());
                } else {
                    StationReport stationReport = stationReportRepository.selectReport(station.getId());
                    if (null != stationReport) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 186L);
                        if (null != stationColorReport && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            stationInfoList.add(stationColorReport.getAnswerOther());
                        } else {
                            stationInfoList.add(value);
                        }
                    } else {
                        stationInfoList.add(value);
                    }
                }
                //获得客户状态
                if (station.getCustomerStat() != null && station.getCustomerStat() != 0) {
                    value = getStationAnswer(String.valueOf(station.getCustomerStat()), report);
                    stationInfoList.add(value);
                    value = "";
                } else {
                    stationInfoList.add(value);
                }
                //获得客户类型
                if (station.getCustomerType() != null && station.getCustomerType() != 0) {
                    StationColorReport stationColorReport = stationColorReportRepository.selectReport(report.getId(), 185L);
                    if (null != stationColorReport && StringUtils.isNotEmpty(stationColorReport.getAnswerId())) {
                        QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(stationColorReport.getAnswerId()));
                        if (null != questionAnswerOption && StringUtils.isNotEmpty(questionAnswerOption.getName())) {
                            value = questionAnswerOption.getName();
                        }
                    }
                    stationInfoList.add(value);
                    value = "";
                } else {
                    stationInfoList.add(value);
                }
                //获得联系人姓名
                if (station.getUser() != null) {
                    userProfile = userProfileRepository.findByUserId(station.getUser().getId());
                    if (userProfile != null && StringUtils.isNotEmpty(userProfile.getRealName())) {
                        stationInfoList.add(userProfile.getRealName());
                    } else {
                        stationInfoList.add(value);
                    }
                } else {
                    stationInfoList.add(value);
                }
                //获得联系人职位
                if (station.getUser() != null) {
                    if (userProfile != null && StringUtils.isNotEmpty(userProfile.getPosition())) {
                        stationInfoList.add(userProfile.getPosition());
                    } else {
                        stationInfoList.add(value);
                    }
                } else {
                    stationInfoList.add(value);
                }
                //获得联系人手机
                if (station.getUser() != null) {
                    if (userProfile != null && StringUtils.isNotEmpty(userProfile.getTelephone())) {
                        stationInfoList.add(userProfile.getTelephone());
                    } else {
                        stationInfoList.add(value);
                    }
                } else {
                    stationInfoList.add(value);
                }
                //获得维修站类型
                if (StringUtils.isNotEmpty(station.getCategory())) {
                    stationInfoList.add(station.getCategory());
                } else {
                    stationInfoList.add(value);
                }
                //获得具体品牌
                StringBuffer stringBuffer = new StringBuffer("");
                StationColorReport colorReport = stationColorReportRepository.selectReport(report.getId(), 8L);
                if (null != colorReport && StringUtils.isNotEmpty(colorReport.getAnswerId())) {
                    String[] ids = colorReport.getAnswerId().split(",");
                    for (String id : ids) {
                        QuestionAnswerOption answer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                        if (null != answer && StringUtils.isNotEmpty(answer.getName())) {
                            stringBuffer.append(answer.getName());
                        }
                    }
                    stationInfoList.add(stringBuffer.toString());
                } else {
                    stationInfoList.add("");
                }
                //获取其他品牌
                if (null != colorReport && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
                    stationInfoList.add(colorReport.getAnswerOther());
                } else {
                    stationInfoList.add("");
                }

                //获取维修站所属集团
                stationInfoList.add(getColorOtherAnswer(report.getId(), 9L));
                //获取该维修站每月钣喷维修钣件数
                stationInfoList.add(getColorOtherAnswer(report.getId(), 10L));
                //每月钣喷维修台次
                stationInfoList.add(getColorOtherAnswer(report.getId(), 11L));
                //该维修站油漆月用量
                stationInfoList.add(getColorOtherAnswer(report.getId(), 12L));
                //油漆品牌
                stationInfoList.add(getColorAnswer(report.getId(), 13L));
                //选择其他油漆品牌的请说明
                stationInfoList.add(getColorOtherAnswer(report.getId(), 13L));
                //品牌定位
                stationInfoList.add(getColorAnswer(report.getId(), 182L));
                //油漆服务商
                stationInfoList.add(getColorOtherAnswer(report.getId(), 14L));
                //前处理人数
                stationInfoList.add(getColorOtherAnswer(report.getId(), 15L));
                //面漆人数
                stationInfoList.add(getColorOtherAnswer(report.getId(), 16L));
                //抛光人数
                stationInfoList.add(getColorOtherAnswer(report.getId(), 17L));
                //调漆人数
                stationInfoList.add(getColorOtherAnswer(report.getId(), 18L));
                //喷漆房数量（个）
                stationInfoList.add(getColorOtherAnswer(report.getId(), 19L));
                //气压
                stationInfoList.add(getColorAnswer(report.getId(), 20L));
                //光亮度
                stationInfoList.add(getColorAnswer(report.getId(), 21L));
                //光亮度备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 21L));
                //洁净度
                stationInfoList.add(getColorAnswer(report.getId(), 39L));
                //洁净度备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 39L));
                //可否进行加温
                stationInfoList.add(getColorAnswer(report.getId(), 22L));
                //加温类型
                stationInfoList.add(getColorAnswer(report.getId(), 23L));
                //加温类型备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 23L));
                //空压机类型
                stationInfoList.add(getColorAnswer(report.getId(), 24L));
                //是否安装冷干机
                stationInfoList.add(getColorAnswer(report.getId(), 25L));
                //压缩空气洁净度
                stationInfoList.add(getColorAnswer(report.getId(), 26L));
                //喷漆房内有没有油水分离器
                stationInfoList.add(getColorAnswer(report.getId(), 27L));
                //打磨设备类型
                stationInfoList.add(getColorAnswer(report.getId(), 28L));
                //圆形打磨机数量
                stationInfoList.add(getColorOtherAnswer(report.getId(), 29L));
                //长方形打磨机数量
                stationInfoList.add(getColorOtherAnswer(report.getId(), 30L));
                //打磨机质量
                stationInfoList.add(getColorAnswer(report.getId(), 32L));
                //打磨机吸尘能力
                stationInfoList.add(getColorAnswer(report.getId(), 33L));
                //该维修站红外线烤灯状况，配备台数
                stationInfoList.add(getColorOtherAnswer(report.getId(), 34L));
                //HVLP喷枪（把）
                stationInfoList.add(getColorOtherAnswer(report.getId(), 35L));
                //传统喷枪（把）
                stationInfoList.add(getColorOtherAnswer(report.getId(), 36L));
                //中涂喷枪（把）
                stationInfoList.add(getColorOtherAnswer(report.getId(), 37L));
                //喷枪喷涂质量
                stationInfoList.add(getColorAnswer(report.getId(), 38L));
                //底漆品牌
                stationInfoList.add(getColorOtherAnswer(report.getId(), 40L));
                //底漆型号
                stationInfoList.add(getColorOtherAnswer(report.getId(), 41L));
                //原子灰品牌
                stationInfoList.add(getColorOtherAnswer(report.getId(), 42L));
                //原子灰型号
                stationInfoList.add(getColorOtherAnswer(report.getId(), 43L));
                //中涂底漆品牌
                stationInfoList.add(getColorOtherAnswer(report.getId(), 44L));
                //中涂底漆型号
                stationInfoList.add(getColorOtherAnswer(report.getId(), 45L));
                //底色漆品牌
                stationInfoList.add(getColorOtherAnswer(report.getId(), 46L));
                //底色漆型号
                stationInfoList.add(getColorOtherAnswer(report.getId(), 47L));
                //清漆品牌
                stationInfoList.add(getColorOtherAnswer(report.getId(), 48L));
                //清漆型号
                stationInfoList.add(getColorOtherAnswer(report.getId(), 49L));
                //中涂底漆前是否使用水磨
                stationInfoList.add(getColorAnswer(report.getId(), 50L));
                //颜色准确度
                stationInfoList.add(getColorAnswer(report.getId(), 51L));
                //颜色准确度备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 51L));
                //最常用的颜色查询工具
                stationInfoList.add(getColorAnswer(report.getId(), 52L));
                //颜色查询工具备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 52L));
                //AkzoNobel服务商人员来店服务时使用的颜色工具
                stationInfoList.add(getColorAnswer(report.getId(), 53L));
                //颜色工具备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 53L));
                //返工率最高的颜色
                stationInfoList.add(getColorOtherAnswer(report.getId(), 54L));
                //配方非常稳定的颜色
                stationInfoList.add(getColorOtherAnswer(report.getId(), 55L));
                //问题类型
                stationInfoList.add(getColorAnswer(report.getId(), 56L));
                //问题类型备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 56L));
                //问题类型具体说明
                stationInfoList.add(getColorOtherAnswer(report.getId(), 57L));
                //诉求类型
                stationInfoList.add(getColorAnswer(report.getId(), 59L));
                //诉求类型备注
                stationInfoList.add(getColorOtherAnswer(report.getId(), 59L));
                //诉求类型具体说明
                stationInfoList.add(getColorOtherAnswer(report.getId(), 60L));
            } else {
                logger.info("没有维修站的报告ID为" + report.getId());
                continue;
            }
            stationInfoLists.add(stationInfoList);
        }
        return stationInfoLists;
    }

    private String getStationAnswer(String answerId, StationReport stationReport) {
        String[] ids = answerId.split(",");
        StringBuffer stringBuffer = new StringBuffer("");
        for (String id : ids) {
            if (StringUtils.isNotEmpty(id)) {
                //判断客户状态是否是其他
                if (id.equals("362")) {
                    StationColorReport colorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                    if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
                        stringBuffer.append(colorReport.getAnswerOther() + ",");
                    }
                } else if (id.equals("366")) {
                    StationColorReport colorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                    if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
                        stringBuffer.append(colorReport.getAnswerOther() + ",");
                    }
                } else {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                    if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                        stringBuffer.append(answerOption.getName() + ",");
                    }
                }
            }
        }
        return stringBuffer.toString();
    }

    private String getColorAnswer(Long reportId, Long questionId) {
        StationColorReport colorReport = stationColorReportRepository.selectReport(reportId, questionId);
        StringBuffer stringBuffer = new StringBuffer("");
        if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerId())) {
            String[] ids = colorReport.getAnswerId().split(",");
            for (String id : ids) {
                if (StringUtils.isNotEmpty(id)) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                    if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                        stringBuffer.append(answerOption.getName() + ",");
                    }
                }
            }
        } else if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
            stringBuffer.append(colorReport.getAnswerOther() + ",");
        }
        if (stringBuffer.indexOf("请详细说明") != -1 && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
            stringBuffer.toString().replace("请详细说明", colorReport.getAnswerOther());
        }
        return stringBuffer.toString();
    }

    private String getColorOtherAnswer(Long reportId, Long questionId) {
        StationColorReport colorReport = stationColorReportRepository.selectReport(reportId, questionId);
        if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
            return colorReport.getAnswerOther();
        }
        return "";
    }


    private static final Logger logger = LoggerFactory.getLogger(StationServiceImpl.class);


}
